package cn.slt.controller;

import java.io.Console;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.slt.domian.MD_ClassDSID;
import cn.slt.domian.MD_Metadata;
import cn.slt.domian.MD_libraryDSID;
import cn.slt.domian.users;
import cn.slt.service.MD_MetadataService;
import cn.slt.service.MD_classDSIDService;
import cn.slt.service.MD_libraryDSIDService;

@Controller

public class MD_MetadataController {
	@Autowired
	private MD_MetadataService service;
	@Autowired
	private MD_libraryDSIDService md_libraryDSIDService;
	@Autowired
	private MD_classDSIDService MD_classDSIDService;

	@ResponseBody
	@RequestMapping("addname")
	public String addname(@RequestParam String mdname, @RequestParam String firstlev, @RequestParam String secondlev,
			@RequestParam String thirdlev, @RequestParam String owner, @RequestParam String location,
			@RequestParam String buildunit, @RequestParam String staff, @RequestParam String database) {
		boolean mark = false;
		String words = null;
		String areacode = "320000" + "00";
		String fourthlev = "00";
		String fifthlev = "00";
		String url = null;
		if (database.equals("oracle")) {
			url = "jdbc:oracle:thin:@";
		} else {
			url = "jdbc:jtds:sqlserver://";
		}

		MD_Metadata obj = new MD_Metadata();
		String mdCode = new String("SLMD2016");
		mdCode = mdCode + areacode + firstlev + secondlev + thirdlev + fourthlev + fifthlev;
		location = location.trim();

		owner = owner.trim();

		obj.setMdCode(mdCode);
		obj.setMdName(mdname);
		obj.setAreaCod(areacode);
		obj.setOwner(owner.toUpperCase());
		obj.setFirstlev(firstlev);
		obj.setSecondlev(secondlev);
		obj.setThirdlev(thirdlev);
		obj.setFourthlev(fourthlev);
		obj.setFifthlev(fifthlev);
		obj.setStaff(staff);
		obj.setBuildUnit(buildunit);
		obj.setLocation(url + location);

		System.out.println(obj.toString());
		try {
			mark = service.add(obj);
			System.out.println("yichang1");
			if (mark) {
				words = "1";
			} else {
				words = "2";
			}
		} catch (Exception e) {
			words = "0";
			System.out.println("yichang");
			e.printStackTrace();
		} finally {
			return words;
		}

	}

	@ResponseBody
	@RequestMapping("updatename")
	public String updatename(@RequestParam String mdname, @RequestParam String firstlev, @RequestParam String secondlev,
			@RequestParam String thirdlev, @RequestParam String owner, @RequestParam String location,
			@RequestParam String buildunit, @RequestParam String staff, @RequestParam String database) {
		boolean mark = false;
		String words = null;
		String areacode = "320000" + "00";
		String fourthlev = "00";
		String fifthlev = "00";
		String url = null;
		if (database.equals("oracle")) {
			url = "jdbc:oracle:thin:@";
		} else {
			url = "jdbc:jtds:sqlserver://";
		}

		MD_Metadata obj = new MD_Metadata();
		MD_libraryDSID libraryDSID=new MD_libraryDSID();
		users users=new users();
		String mdCode = new String("SLMD2016");
		mdCode = mdCode + areacode + firstlev + secondlev + thirdlev + fourthlev + fifthlev;
		location = location.trim();

		owner = owner.trim();

		obj.setMdCode(mdCode);
		obj.setMdName(mdname);
		obj.setAreaCod(areacode);
		obj.setOwner(owner.toUpperCase());
		obj.setFirstlev(firstlev);
		obj.setSecondlev(secondlev);
		obj.setThirdlev(thirdlev);
		obj.setFourthlev(fourthlev);
		obj.setFifthlev(fifthlev);
		obj.setStaff(staff);
		obj.setBuildUnit(buildunit);
		obj.setLocation(location);

		System.out.println(obj.toString());
		try {
			mark = service.updateMeta(obj);
			Map<String, String> map = new HashMap<String, String>();
			map.put("location", obj.getLocation());
			map.put("dsName", obj.getOwner().trim());
			Integer dscode = null;
			try {
				dscode = md_libraryDSIDService.getdscode(map);
			} catch (Exception e) {
				// TODO: handle exception
			}
			List<MD_ClassDSID> mDsids = null;
			try {
				mDsids = MD_classDSIDService.getBySupdscode(dscode);
			} catch (Exception e) {
				// TODO: handle exception
			}
			if (mDsids != null) {

				for (MD_ClassDSID mDsid : mDsids) {
					if (obj.getThirdlev().equals("00")) {
						mDsid.setLevelcode("00");
					}
					MD_classDSIDService.updateclass(mDsid);
				}
			}
			System.out.println("updatemeta ok");
			if (mark) {
				words = "1";
			} else {
				words = "2";
			}
		} catch (Exception e) {
			// words = "0";
			System.out.println("yichang");
			e.printStackTrace();
		} finally {
			return words;
		}

	}
	// @RequestMapping("/addBatch")
	// public void testaddBatch(HttpServletResponse response){
	// System.out.println("control________________________1");
	// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	// List<MD_Metadata> list=new ArrayList<MD_Metadata>();
	// MD_Metadata Metadata=new MD_Metadata();
	// Metadata.setMdCode("111");
	// Metadata.setMdName("test1");
	// Metadata.setAreaCod("0251");
	// Metadata.setBuildUnit("河海大学");
	// Metadata.setStaff("上司");
	// try {
	// Metadata.setBuildDate(sdf.parse("2016-05-06"));
	// } catch (ParseException e) {
	// e.printStackTrace();
	// }
	// Metadata.setStandName("标准名称");
	// Metadata.setStandVers("verson1.0");
	// Metadata.setLanguage("001");
	// Metadata.setCharSet("001");
	// list.add(Metadata);
	//
	// MD_Metadata Metadata1=new MD_Metadata();
	// Metadata1.setMdCode("112");
	// Metadata1.setMdName("test2");
	// Metadata1.setAreaCod("0252");
	// Metadata1.setBuildUnit("河海大学");
	// Metadata1.setStaff("上司");
	// try {
	// Metadata1.setBuildDate(sdf.parse("2016-05-06"));
	// } catch (ParseException e) {
	// e.printStackTrace();
	// }
	// Metadata1.setStandName("标准名称");
	// Metadata1.setStandVers("verson1.1");
	// Metadata1.setLanguage("002");
	// Metadata1.setCharSet("002");
	// list.add(Metadata1);
	//
	// MD_Metadata Metadata2=new MD_Metadata();
	// Metadata2.setMdCode("113");
	// Metadata2.setMdName("test2");
	// Metadata2.setAreaCod("0252");
	// Metadata2.setBuildUnit("河海大学");
	// Metadata2.setStaff("上司");
	// try {
	// Metadata2.setBuildDate(sdf.parse("2016-05-06"));
	// } catch (ParseException e) {
	// e.printStackTrace();
	// }
	// Metadata2.setStandName("标准名称");
	// Metadata2.setStandVers("verson1.1");
	// Metadata2.setLanguage("002");
	// Metadata2.setCharSet("002");
	// list.add(Metadata2);
	//
	// boolean mark=service.addBatch(list);
	// if(mark){
	// try {
	// response.getWriter().print(1);
	// } catch (IOException e) {
	// e.printStackTrace();
	// }
	// }
	// else{
	// try {
	// response.getWriter().print(0);
	// } catch (IOException e) {
	// e.printStackTrace();
	// }
	// }
	// }
	//
	// @RequestMapping("/add")
	// public String addMetadata(MD_Metadata Metadata){ //Metadata出了上个方法的作用域唉
	// System.out.println("control_________2");
	// List<MD_Metadata> list2=new ArrayList<MD_Metadata>();
	// list2.add(Metadata);
	// boolean mark=service.addBatch(list2);
	// if(mark){
	// return "jsp/success";
	// }
	// else{
	// return "jsp/error";
	// }
	// }
}
